package com.happe.foodeasy.winter.typemap;

import com.happe.foodeasy.constraint.annotation.StringTypeMetadata;

import java.lang.reflect.Field;

/**
 * This class is used to map java string type to corresponding database type.
 * Created by wubin on 2016-05-20.
 */
public class StringTypeMapDeal extends TypeMapDeal {
    //if the target field doesn't have StringAnnotation, will use this length.
    private static final int DEFAULT_STRING_LENGTH = 50;

    @Override
    public String retrieveMapType(Field field) {
        StringTypeMetadata stringTypeMetadata = field.getAnnotation(StringTypeMetadata.class);
        int length;
        if (stringTypeMetadata == null) {
            length = DEFAULT_STRING_LENGTH;
        } else {
            length = stringTypeMetadata.typeLength();
        }
        return getDatabaseTypeMapParseResult().getStringType(length);
    }
}
